package com.atguigu.gmall.publisher.controller;

import com.atguigu.gmall.publisher.beans.TradeProvinceOrderAmount;
import com.atguigu.gmall.publisher.service.TradeStatsService;
import com.atguigu.gmall.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Felix
 * @date 2023/4/26
 * 交易域统计Controller
 * @Controller 将类的实例化交给Spring容器，如果类中的方法返回的是字符串，会进行页面的跳转;如果不做页面跳转，只返回
 *              字符串给客户端，需要在方法上加上@ResponseBody
 * @RestController 将类的实例化交给Spring容器，如果类中的方法返回的是字符串,不会进行页面的跳转，直接将字符串返回给
 *               客户端。
 * @RestController = @Controller + @ResponseBody
 * @RequestMapping("/gmv") 拦截请求，将请求交给标注的方法进行处理
 */
@RestController
public class TradeStatsController {

    @Autowired
    private TradeStatsService tradeStatsService;

    @RequestMapping("/gmv")
    public String getGmv(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            //如果请求中没有携带日期参数，将当天作为查询的日期
            date = DateFormatUtil.now();
        }
        BigDecimal gmv = tradeStatsService.getGMV(date);
        String json = "{\"status\": 0,\"data\": "+ gmv +"}";
        return json;
    }

    /*@RequestMapping("/province")
    public String getProvinceAmount(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            date = DateFormatUtil.now();
        }
        List<TradeProvinceOrderAmount> provinceOrderAmountList = tradeStatsService.getProvinceAmount(date);
        StringBuilder jsonB = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < provinceOrderAmountList.size(); i++) {
            TradeProvinceOrderAmount provinceAmount = provinceOrderAmountList.get(i);
            jsonB.append("{\"name\": \""+provinceAmount.getProvinceName()+"\",\"value\": "+provinceAmount.getOrderAmount()+"}");
            if(i < provinceOrderAmountList.size() - 1){
                jsonB.append(",");
            }
        }
        jsonB.append("],\"valueName\": \"交易额\"}}");
        return jsonB.toString();
    }*/

    @RequestMapping("/province")
    public Map getProvinceAmount(@RequestParam(value = "date",defaultValue = "0") Integer date){
        if(date == 0){
            date = DateFormatUtil.now();
        }

        Map resMap = new HashMap();
        resMap.put("status",0);
        Map dataMap = new HashMap();
        List dataList = new ArrayList();

        List<TradeProvinceOrderAmount> provinceOrderAmountList = tradeStatsService.getProvinceAmount(date);
        for (TradeProvinceOrderAmount provinceOrderAmount : provinceOrderAmountList) {
            Map objMap = new HashMap();
            objMap.put("name",provinceOrderAmount.getProvinceName());
            objMap.put("value",provinceOrderAmount.getOrderAmount());
            dataList.add(objMap);
        }
        dataMap.put("mapData",dataList);
        dataMap.put("valueName","交易额");
        resMap.put("data",dataMap);
        return resMap;
    }
}